package ua.races.jsptag;

import ua.races.dao.RacesDaoImpl;
import org.apache.log4j.Logger;
import ua.races.dao.RacesDaoImpl;
import ua.races.vo.Horse;
import ua.races.vo.Race;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

public class RacesForBetTag extends TagSupport {

    private static final Logger log = Logger.getLogger(RacesForBetTag.class.getName());

    @Override
    public int doStartTag() throws JspException {
        List<Race> races = null;
        final List<Horse> horses;
        RacesDaoImpl dao;

        dao = (RacesDaoImpl) pageContext.getServletContext().getAttribute("DAO");

        try {
            races = dao.findAllRacesForBet();
            horses = dao.findAllHorses();

        } catch (SQLException e) {
            log.error(e);
        }

        JspWriter w = pageContext.getOut();
        try {
            w.println("<form action=\"addNewBet.do\"  method=\"POST\">");
            w.println("<table summary=\"\" border=\"1\" bgcolor=\"#add8e6\">");
            w.println("<thead bgcolor = \"#e9967a\" >");
            w.println("<tr >\n" +
                    "        <th > Date </th >\n" +
                    "        <th > Time </th >\n" +
                    "        <th > Stadium </th >\n" +
                    "        <th > Info </th >\n" +
                    "</tr >\n" +
                    "</thead >\n" +
                    "<tbody>");
            for (Race r : races) {
                w.println("<tr >\n" +
                        "        <td ><input type = \"hidden\"" +
                        "        name = \"raceId\"" +
                        "        value = \"" + r.getRaceId() + "\">" + r.getRaceDate() +
                        "</td>");
                //w.println("<td>" + r.getRaceDate() + "</td>");
                w.println("<td>" + r.getStartTime() + "</td>");
                w.println("<td>" + r.getStadium() + "</td>");
                w.println("<td ><input type = \"submit\"" +
                        " value = \"bet it!\" / ></td >\n</tr>");
            }

            w.println("</tbody >\n" +
                    "        </table > " +
                    "</form>");

        } catch (IOException e) {
            log.error(e);
        }

        return SKIP_BODY;

    }

    @Override
    public int doEndTag() throws JspException {
        return EVAL_PAGE;
    }
}
